package com.panwenliang2008.eatwhat.ui.select

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.panwenliang2008.eatwhat.R
import com.panwenliang2008.eatwhat.data.entity.Food
import com.panwenliang2008.eatwhat.databinding.SelectFragmentBinding
import com.panwenliang2008.eatwhat.utilities.InjectorUtils
import kotlinx.android.synthetic.main.select_fragment.*
import kotlinx.coroutines.*
import rubikstudio.library.model.LuckyItem
import java.util.*


class SelectFragment : Fragment() {

    private var _binding: SelectFragmentBinding? = null
    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    private val luckyItemList: MutableList<LuckyItem> = ArrayList()
    private val foodList: MutableList<Food> = ArrayList()

    private val selectViewModel: SelectViewModel by viewModels {
        InjectorUtils.provideSelectViewModelFactory(this)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = SelectFragmentBinding.inflate(inflater, container, false)

        initView()

        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        Glide.with(this).asGif()
            .load(R.drawable.giphy)
            .into(imageView)

    }

    private fun initView() {

        binding.apply {
            viewModel = selectViewModel
            lifecycleOwner = viewLifecycleOwner
            callback = object : UpdateCallback {
                //点击按钮之后换一个随机食物
                override fun updateSelectFood() {
//                    textView.text = ""
//                    CoroutineScope(Dispatchers.IO).launch {
//                        delay(500)
//                        withContext(Dispatchers.Main) {
//                            showRandomFood()
//                        }
//                    }
                    binding.luckyWheel?.setRound(5)
                    binding.luckyWheel?.startLuckyWheelWithTargetIndex(Random().nextInt(foodList.size))
                }
            }

        }

        //横竖屏切换的时候执行
        selectViewModel.getSelectedFoodSavedState().observe(viewLifecycleOwner, Observer {
            if (it.isNullOrEmpty()) {
                //显示一个随机食物
                showRandomFood()
            } else {
                binding.textView.text = it
            }
        })

        //点击添加按钮跳转到添加界面
        binding.fabSet.setOnClickListener {
            navigateToSelectOptionFragment()
        }

        initLuckyWheel()
    }

    private fun initLuckyWheel() {

        selectViewModel.getAll().observe(viewLifecycleOwner, Observer {
            luckyItemList.clear()
            foodList.clear()
            foodList.addAll(it)
            it.forEach { food ->
                val luckyItem = LuckyItem()
                luckyItem.topText = food.foodName
                luckyItem.color = -0xc20
                luckyItemList.add(luckyItem)
            }

            binding.luckyWheel?.setData(luckyItemList)
            binding.luckyWheel?.setRound(0)
            binding.luckyWheel?.startLuckyWheelWithTargetIndex(Random().nextInt(it.size))
        })

        binding.luckyWheel?.setLuckyRoundItemSelectedListener {
            binding.textView.text = luckyItemList[it].topText
        }
    }

    private fun navigateToSelectOptionFragment() {
        findNavController().navigate(R.id.action_selectFragment_to_selectOptionFragment)
    }

    private fun showRandomFood() {
        selectViewModel.updateRandomFood().observe(viewLifecycleOwner, Observer {
            if (it != null) {
                selectViewModel.setSelectedFoodSavedState(it.foodName)
            }
        })
    }

    interface UpdateCallback {
        fun updateSelectFood()
    }
}
